shell练习- expect配合telnet批量测试远端端口

linux expect配合telnet批量测试远端端口

host.txt文件如下

1
2
3
4
5
6
7
172.16.207.128  22
172.16.207.129  22
172.16.207.131  80
172.16.207.132  22
172.16.207.132  8080
172.16.207.132  8005
192.168.0.222   333

测试端口脚本如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash
HOST=/root/host.txt
OUTPUT=/tmp/telnet_error.log
#############################
echo starting
echo "telnet error!!!"> $OUTPUT
while read ip
do
(
echo $ip|grep  "^[0-9]" >/dev/nullvi
if [ $? -eq 0 ];then
                expect <<EOF  > "$ip"
                set timeout 3;
                spawn telnet $ip
                expect eof
EOF
                grep "Escape character" "$ip" >/dev/null
                if [ ! $? -eq 0 ];then
                grep 'spawn' "$ip" |awk '$1=$2=" "{print}' >> $OUTPUT
                fi
                rm -rf "$ip"
fi
)&


done<$HOST
wait
echo "Output:$OUTPUT"
cat $OUTPUT